package com.suixun.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suixun.dao.batchmanage.BatchServiceDAO;
import com.suixun.dao.devicemanage.DeviceInitServiceDAO;
import com.suixun.entity.BatchInfo;
import com.suixun.entity.DeviceInfo;
import com.suixun.entity.form.ParamForm;
import com.suixun.service.BatchService;
import com.suixun.util.StringUtil;
import com.suixun.vo.BatchVO;

@Service
public class BatchServiceImpl implements BatchService {

	@Resource
	private BatchServiceDAO batchdao;
	
	@Resource
	private DeviceInitServiceDAO deviceInitdao;


	// 查询某个批号明细
	@Override
	public BatchInfo queryBatchByID(ParamForm paramForm) {
		if (StringUtil.isBlank(paramForm.getId()))
			return null;
		BatchInfo batch = batchdao.queryBatchByID(paramForm);
		return batch;
	}

	// 查询批号里面的设备数量
	@Override
	public int queryBatchDeviceCounts(ParamForm paramForm) {
		if (StringUtil.isBlank(paramForm.getId()))
			return -1;
		return batchdao.queryBatchDeviceCounts(paramForm);
	}

	// 查询批号列表
	@Override
	public List<BatchVO> queryBatchlist(ParamForm paramForm) {
		if (paramForm != null && !StringUtil.isBlank(paramForm.getBatchid())) {
			paramForm.setBatchid(StringUtil.append(paramForm.getBatchid()));
		}
		List<BatchInfo> devicelist = batchdao.queryBatchlist(paramForm);
		BatchVO batchVO = new BatchVO();
		return batchVO.batchInfoToVO(devicelist);
	}


	// 新增批号
	@Override
	@Transactional
	public int addBatch(ParamForm paramForm) {
		Map<String,Object> map = new HashMap<>();
		map.put("batchid", paramForm.getBatchid());
		int result = batchdao.addBatch(map);
		return result;
	}

	// 修改批号
	@Override
	@Transactional
	public int modifyBatch(ParamForm paramForm) {
		if (paramForm == null || paramForm.getId() == null || StringUtil.isBlank(paramForm.getBatchid()) || StringUtil.isBlank(paramForm.getOldbatchid())) {
			return 0;
		}
		Map<String,Object> map = new HashMap<>();
		map.put("id", Integer.parseInt(paramForm.getId()));
		map.put("batchid", paramForm.getBatchid());
		BatchInfo batchInfo = batchdao.queryBatchByID(paramForm);
		if(batchInfo == null ) {
			return updateDevice(paramForm);
		}else {
			// 更新批号信息表
			int result = batchdao.updateBatch(map);
			updateDevice(paramForm);
			return result;
		}
	}
	
	public int updateDevice(ParamForm paramForm) {
		Map<String,Object> map = new HashMap<>();
		map.put("batchid", paramForm.getOldbatchid());
		//查询批号下面的设备
		List<DeviceInfo> deviceList = deviceInitdao.queryDeviceInitlist(map);
		map.put("batchid", paramForm.getBatchid());
		int result = 0;
		if(deviceList.size()>0) {
			map.put("deviceList", deviceList);
			result = deviceInitdao.updateDevice(map);
		}
		return result;
	}

	// 删除批号
	@Override
	@Transactional
	public int delBatch(int id) {
		int result = batchdao.deleteBatch(id);
		return result;
	}

	/**
	 * <p>Title: queryBatchByCode</p>
	 * <p> 功能描述: 查询批号</p>
	 * @param paramForm
	 * @return
	 */
	@Override
	public List<BatchInfo> queryBatchByCode(ParamForm paramForm) {
		if (StringUtil.isBlank(paramForm.getBatchid())) {
			return null;
		}
		List<BatchInfo> batchList = batchdao.queryBatchByCode(paramForm);
		return batchList;
	}

}
